package com.spirng.leetcode.day05;

import java.util.Deque;
import java.util.LinkedList;

public class Test1047 {
    public static void main(String[] args) {
      removeDuplicates("abbaca");
    }
    public static String removeDuplicates(String s) {
        Deque<Character> deque = new LinkedList<>();
        char[] sCharArray = s.toCharArray();
        for (int i = 0; i < sCharArray.length; i++) {
            if (deque.peekFirst()==null||sCharArray[i]!=deque.peekFirst()) {
                deque.addFirst(sCharArray[i]);
            }else {
                deque.removeFirst();
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int j=deque.size();j>0;j--) {
            stringBuilder.append(deque.getLast());
            deque.removeLast();
        }
        return stringBuilder.toString();
    }
}
